home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / Bar Charts of arbitrary size.php < prev    next >
Encoding:
PHP Script  |  2001-07-02  |  8.9 KB  |  220 lines

  1.     //**************************************
  2.     //     
  3.     // Name: Bar Charts of arbitrary size
  4.     // Description:Both PHP2 and PHP3 versio
  5.     //     ns are now available. This code creates 
  6.     //     bar charts of arbirary size, color and n
  7.     //     umber. It works with gd1.2 and 1.3 Full 
  8.     //     description at http://www.jeo.net/php/ b
  9.     //     y Afan Ottenheimer
  10.     // By: PHP Code Exchange
  11.     //**************************************
  12.     //     
  13.     
  14.     <?php
  15.     /* Copyright (C) 1998 Afan Ottenheimer, afan@jeonet.com
  16.     This is distributed with NO WARRANTY and under the terms of the GNU GPL.
  17.     There is one exception - a license is required if this is incorporated 
  18.     into, or sold or distributed with a Microsoft product.
  19.     You can get a copy of the GNU GPL at http://www.gnu.org/copyleft/gpl.html
  20.     */
  21.     /* Some comments about the program: 
  22.     This creates a chart with the thought that the X axis is 
  23.     labeled array and the Y axis is real numbers. The Y axis uses a
  24.     conversion between graph and data units called n_max_y.
  25.     The X axis has no such conversion. Each x-axis-tick can get its
  26.     own label xlab[N], where N= an integer.
  27.     */
  28.     /* READ DATA HERE */
  29.     $maxy = $maxy_in;
  30.     if ($maxy == 0) {
  31.     echo "Error Max Y = 0 <p>";
  32.     exit;
  33.     }
  34.     SetType($maxy,"double");
  35.     /* The x-axis is divided up into $maxx sections going from 0 to $maxx-1 */
  36.     $maxx = $maxx_in;
  37.     if ($maxx <= 0) {
  38.     echo "error maxx <= 0 <p>";
  39.     exit;
  40.     }
  41.     SetType($maxx,"double");
  42.     $XSIZE = $XSIZE_in; /* full width of the image in pixels */
  43.     SetType($XSIZE,"double");
  44.     $YSIZE = $YSIZE_in; /* Full height of the image in pixels */
  45.     SetType($YSIZE,"double");
  46.     $num_vert_ticks = $num_vert_ticks_in;
  47.     if ($num_vert_ticks <= 0) {
  48.     echo "error num_vert_ticks <= 0 <p>";
  49.     exit;
  50.     }
  51.     SetType($num_vert_ticks,"double");
  52.     $y_title = $y_title_in;
  53.     $x_title = $x_title_in;
  54.     /* *********END DATA ********** */
  55.     /* We have the Header/Content-type after any error messages that might occur */
  56.     Header("Content-Type: image/gif"); 
  57.     $x_tot_margin = 100.0;
  58.     SetType($x_tot_margin,"double");
  59.     $x_left_margin = 77.0; /* distance between left and start of x axis in pixels */
  60.     SetType($x_left_margin,"double");
  61.     /* The x-axis is divided up into $maxx sections going from 0 to $maxx-1 */
  62.     $xscale = ($XSIZE - $x_tot_margin)/$maxx ; 
  63.     SetType($xscale,"double");
  64.     $y_top_margin = 14.0; /* Must be integer */
  65.     SetType($y_top_margin,"double");
  66.     $y_tot_margin = 80.0; /* Must be integer */
  67.     SetType($y_tot_margin,"double");
  68.     $yscale = ($YSIZE - $y_tot_margin)/$maxy; /* Maximum Height in Screen Units */
  69.     SetType($yscale,"double");
  70.     $y_bot_margin = $y_tot_margin - $y_top_margin;
  71.     SetType($y_bot_margin,"double");
  72.     /* The y-axis is divided up into sections going from 0 to $maxy */
  73.     $y_tick_delta = $maxy / $num_vert_ticks;
  74.     SetType($y_tick_delta,"double");
  75.     $si_units[0] = ""; /* Not yet used */
  76.     $si_units[1] = "k";
  77.     $si_units[2] = "M";
  78.     $si_units[3] = "G";
  79.     $si_units[4] = "T";
  80.     $small_font = 2; /* fonts = 1,2,3,4 or 5 */
  81.     $small_font_width = 6.0; /* width in pixels */
  82.     $small_font_height = 10.0; /* height in pixels */
  83.     $tiny_font = 1;
  84.     $c_blank[0] = "245";
  85.     $c_blank[1] = "245";
  86.     $c_blank[2] = "245";
  87.     $c_light[0] = "194";
  88.     $c_light[1] = "194";
  89.     $c_light[2] = "194";
  90.     $c_dark[0] = "100";
  91.     $c_dark[1] = "100";
  92.     $c_dark[2] = "100";
  93.     $c_major[0] = "255";
  94.     $c_major[1] = "0";
  95.     $c_major[2] = "0";
  96.     $c_grid[0] = "0";
  97.     $c_grid[1] = "0";
  98.     $c_grid[2] = "0";
  99.     $col_in[0] = "0";
  100.     $col_in[1] = "235";
  101.     $col_in[2] = "12";
  102.     $col_inm[0] = "0";
  103.     $col_inm[1] = "166";
  104.     $col_inm[2] = "33";
  105.     $col_out[0] = "0";
  106.     $col_out[1] = "94";
  107.     $col_out[2] = "255";
  108.     $col_outm[0] = "255";
  109.     $col_outm[1] = "0";
  110.     $col_outm[2] = "255";
  111.     /* ################################################# */
  112.     /* Some general definitions for the graph generation */
  113.     $XSIZE = (($maxx*$xscale)+100);
  114.     /* position the graph */
  115.     /* translate x/y coord into pixel coord */
  116.     old_function ytr $y (
  117.     SetType($y,"double");
  118.     global $maxy,$yscale,$y_top_margin;
  119.     return ($maxy*$yscale+$y_top_margin-(($y)*$yscale));
  120.     );
  121.     old_function xtr $x (
  122.     SetType($x,"double");
  123.     global $maxx,$xscale,$growright,$x_left_margin;
  124.     if ($growright) {
  125.     $tmpret = ($maxx*$xscale+$x_left_margin-(($x)*$xscale)) ;
  126.     } else {
  127.     $tmpret = ($x_left_margin+(($x)*$xscale));
  128.     }
  129.     return($tmpret);
  130.     );
  131.     /* ################################################# */
  132.     /* the graph is made ten pixels higher to acomodate the x labels */
  133.     $graph = ImageCreate($XSIZE, $YSIZE);
  134.     /* Use brush_out and brush_outm for ImageSetBrush valid only for GD v3 */
  135.     /* 
  136.     $brush_out = ImageCreate(1,2);
  137.     $brush_outm = ImageCreate(1,2);
  138.     $i_outm = ImageColorAllocate($brush_outm,$col_outm[0], $col_outm[1], $col_outm[2]); 
  139.     $i_out = ImageColorAllocate($brush_out,$col_out[0], $col_out[1], $col_out[2]);
  140.     */
  141.     /* the first color allocated will be the background color. */
  142.     $i_blank = ImageColorAllocate($graph,$c_blank[0],$c_blank[1],$c_blank[2]);
  143.     $i_light = ImageColorAllocate($graph,$c_light[0],$c_light[1],$c_light[2]);
  144.     $i_dark = ImageColorAllocate($graph,$c_dark[0],$c_dark[1],$c_dark[2]);
  145.     /* ImageColorTransparent($graph, $i_blank); */
  146.     /* ImageInterlace($graph, 1); */
  147.     $i_grid = ImageColorAllocate($graph,$c_grid[0],$c_grid[1],$c_grid[2] );
  148.     $i_major = ImageColorAllocate($graph,$c_major[0],$c_major_g,$c_major_b);
  149.     $i_in = ImageColorAllocate($graph,$col_in[0], $col_in[1], $col_in[2]);
  150.     $i_inm = ImageColorAllocate($graph,$col_inm[0], $col_inm[1], $col_inm[2]);
  151.     /* draw the image border */
  152.     ImageLine($graph,0,0,$XSIZE-1,0,$i_light);
  153.     ImageLine($graph,1,1,$XSIZE-2,1,$i_light);
  154.     ImageLine($graph,0,0,0,$YSIZE-1,$i_light);
  155.     ImageLine($graph,1,1,1,$YSIZE-2,$i_light);
  156.     ImageLine($graph,$XSIZE-1,0,$XSIZE-1,$YSIZE-1,$i_dark);
  157.     ImageLine($graph,0,$YSIZE-1,$XSIZE-1,$YSIZE-1,$i_dark);
  158.     ImageLine($graph,$XSIZE-2,1,$XSIZE-2,$YSIZE-2,$i_dark);
  159.     ImageLine($graph,1,$YSIZE-2,$XSIZE-2,$YSIZE-2,$i_dark);
  160.     /* draw the graph border */
  161.     ImageRectangle($graph, xtr(0),ytr(0),xtr($maxx),ytr($maxy),$i_grid);
  162.     /*create a dotted style for the grid lines*/
  163.     /* Style not supported in PHP/FI2 ???
  164.     $styleDotted[0] = $i_grid;
  165.     $styleDotted[1] = Transparent;
  166.     $styleDotted[2] = Transparent;
  167.     ImageSetStyle($graph, $styleDotted, 3);
  168.     */
  169.     /* draw the horizontal grid */
  170.     /* It would be great to know how wide the font is in pixels. In 
  171.     C you would use "small_font->w" for GD. Here we guess as $small_font_width
  172.     */
  173.     ImageStringUp($graph, $small_font,8, ($small_font_width*strlen($y_title)/2.0) + ytr($maxy/2.0), $y_title, $i_grid);
  174.     ImageString($graph, $small_font, -($small_font_width*strlen($x_title)/2.0) + xtr($maxx/2.0) , ($YSIZE - 3.0*$small_font_height), $x_title, $i_grid);
  175.     $i = 0;
  176.     /* Draw Vertical Ticks */
  177.     while ($i<=$num_vert_ticks){
  178.     $y_tmp = (DoubleVal($i)*$maxy/$num_vert_ticks);
  179.     SetType($y_tmp,"double");
  180.     ImageLine($graph,(-10+xtr(0)),ytr($y_tmp),xtr(1),ytr($y_tmp),$i_grid);
  181.     ImageLine($graph,(xtr($maxx)+10),ytr($y_tmp),xtr($maxx-1),ytr($y_tmp),$i_grid);
  182.     /* gdStyled only supported with GD v3 */
  183.     /*ImageLine($graph,xtr(0),ytr($y_tmp),xtr($maxx),ytr($y_tmp),gdStyled); */
  184.     ImageLine($graph,xtr(0),ytr($y_tmp),xtr($maxx),ytr($y_tmp),$i_light);
  185.     $ylab = sprintf("%6.1f %s",$y_tick_delta*$i,$si_units[0]);
  186.     /* 
  187.     It would be great to know the font height in pixels. In 
  188.     C you would use "small_font->h" for GD. Here we guess as $small_font_height
  189.     */
  190.     ImageString($graph, $small_font,($x_left_margin-54),( -($small_font_height/2.0) + ytr($y_tmp)),$ylab, $i_grid);
  191.     $i++;
  192.     } 
  193.     /* draw vertical grid and horizontal labels */
  194.     $x = 0;
  195.     while ($x<$maxx) {
  196.     if ($x_label[$x]) {
  197.     ImageString($graph, $small_font,( -(strlen($x_label[$x]) * $small_font_width / 2.0) + xtr(0.5+$x)), ($YSIZE - $y_bot_margin + 1.5*$small_font_height) ,$x_label[$x], $i_grid);
  198.     }
  199.     if ($y_data[$x] ) { /* Draw a bar */
  200.     ImageFilledRectangle($graph, xtr(0.25+$x), ytr($y_data[$x]), xtr(0.75+$x), ytr(0), $i_major);
  201.     }
  202.     /* FUN DEBUGGING STUFF BELOW */
  203.     /*
  204.     $tempmid = "mid";
  205.     $tempajo = $x;
  206.     ImageString($graph, $small_font, xtr($x), ytr(-1), $tempajo, $i_major);
  207.     ImageString($graph, $small_font, (-((strlen($tempmid))* $small_font_width /2.0) + xtr((.5+$x)) ), ytr(-1), $tempmid, $i_major); 
  208.     */
  209.     /* END FUN DEBUGGING STUFF */
  210.     $x++;
  211.     } 
  212.     ImageGif($graph);
  213.     ImageDestroy($graph);
  214.     /* 
  215.     ImageDestroy($brush_out);
  216.     ImageDestroy($brush_outm);
  217.     */
  218.     ?>
  219.  
  220.